package com.atguigu;


import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description: JDK1.8 时间操作
 * <P> Date: 2022/8/14:18:15
 */

@Slf4j
public class TimeTest {
    public static void main(String[] args) {
        System.out.println(getMonthNoAndWeekNo(LocalDateTime.now().plusDays(1)));
    }


    /**
     * 获取当月第一天
     */
    public static LocalDateTime getFirstLocalDayOfMonth(LocalDateTime localDateTime) {
        return localDateTime.with(TemporalAdjusters.firstDayOfMonth()).with(LocalTime.MIN);
    }

    /**
     * 获取当月第一周  以第一个周一为准
     */
    private static LocalDateTime getFirstMonday(LocalDateTime sourceTime) {
        LocalDateTime firstMondayOfMonth = getFirstLocalDayOfMonth(sourceTime);
        for (int i = 0; i < 6; i++) {
            DayOfWeek dayOfWeekTemp = firstMondayOfMonth.getDayOfWeek();
            if (dayOfWeekTemp.equals(DayOfWeek.MONDAY)) {
                break;
            }
            //往后推一天
            firstMondayOfMonth = firstMondayOfMonth.plusDays(1);
        }
        return firstMondayOfMonth;
    }


    /**
     * 传入当前时间 获取几月份的第几周 [X月份第X周]
     */
    public static String getMonthNoAndWeekNo(LocalDateTime sourceTime) {
//        String monthNoAndWeekNo;
        StringBuilder monthNoAndWeekNo = new StringBuilder();
        //获取当月的第一天
        //获取月第一个周一,从当月第一天开始找
        LocalDateTime firstMondayOfMonth = getFirstMonday(sourceTime);

        //比较当月的第一个星期一 < = 参数时间
        if (firstMondayOfMonth.isBefore(sourceTime) || firstMondayOfMonth.isEqual(sourceTime)) {
            //当月第一个周一在当前时间之前 firstMondayOfMonth<=sourceTime
            //计算两个时间间隔天数
            int dayOfMonthFirstMonday = firstMondayOfMonth.getDayOfMonth();
            int dayOfMonthSourceTime = sourceTime.getDayOfMonth();

            int diffDays = dayOfMonthSourceTime - dayOfMonthFirstMonday;
            //第几周weekNo
            int weekNo = (diffDays / 7) + 1;
            //月份
            int monthNo = sourceTime.getMonth().getValue();

            monthNoAndWeekNo.append(monthNo).append("月份").append("第").append(weekNo).append("周");
        } else {
            //如果当月的第一个周一大于参数时间,则要计算到上个月份去
            //获取上一个月的第一个周一
            LocalDateTime lastMontLocalDateTime = sourceTime.minusMonths(1);
            //上个月的第一天
            //从上个月的第一天开始找周一
            LocalDateTime firstMondayOfMonthLast = getFirstMonday(lastMontLocalDateTime);

            //  计算两个时间间隔天数 (上月第一个周一 减去 当前时间)
            Duration duration = Duration.between(firstMondayOfMonthLast, sourceTime);
            //相差的天数
            long diffDays = duration.toDays();
            //第几周weekNo
            long weekNo = (diffDays / 7) + 1;
            //月份
            int monthNo = firstMondayOfMonthLast.getMonth().getValue();
            //汉字版月份
//            monthNoAndWeekNo = monthNo + "月份" + "第" + weekNo + "周";
            monthNoAndWeekNo.append(monthNo).append("月份").append("第").append(weekNo).append("周");
        }

        return monthNoAndWeekNo.toString();
    }
}
